<template>
  <div class="comments">
    <myheader title="精彩评论">
      <span
        slot="left"
        class="iconfont iconjiantou2"
        @click="$router.back()"
      ></span>
    </myheader>
    <div
      class="lists"
      v-for="(value, index) in comtentList"
      :key="index"
    >
      <div class="item">
        <div class="head">
          <img
            :src="value.header_img"
            alt
          />
          <div>
            <p>{{ value.user.nickname }}</p>
            <span>{{ value.create_date | mydate }}</span>
          </div>
          <span @click="replycomtent(value)">回复</span>
        </div>
        <commentItem
          v-if="value.parent"
          :parent="value.parent"
          @sentcom="replycomtent"
        ></commentItem>
        <div class="text">{{ value.content }}</div>
      </div>
    </div>
    <commentFooter
      :post="articlePost"
      @refresh="footerRes"
      :obj="seccomtent"
      @close="empty"
    ></commentFooter>
  </div>
</template>

<script>
import { mydate } from "@/utils/myfilters.js";
import commentItem from "@/components/commentItem.vue";
import axios from "@/utils/myaxios.js";
import myheader from "@/components/myheader.vue";
import commentFooter from "@/components/commentFooter.vue";
import { post_comment, getArticleById } from "@/apis/article.js";
export default {
  data() {
    return {
      comtentList: [],
      articlePost: {},
      seccomtent: {},
    };
  },
  filters: {
    mydate,
  },
  methods: {
    empty() {
      this.seccomtent = null;
    },
    replycomtent(v) {
      console.log(v);
      this.seccomtent = v;
    },
    async init() {
      let res = await post_comment(this.$route.params.id);
      this.comtentList = res.data.data.map((v) => {
        if (v.header_img) {
          v.header_img = axios.defaults.baseURL + v.header_img;
        } else {
          v.header_img =
            axios.defaults.baseURL + "/uploads/image/IMG1569381163794.jpeg";
        }
        return v;
      });
    },
    footerRes() {
      this.init();
      this.articlePost.comment_length++;
      this.seccomtent = null;
    },
  },
  async mounted() {
    this.init();

    let articledata = await getArticleById(this.$route.params.id);
    this.articlePost = articledata.data.data;
  },
  components: {
    myheader,
    commentItem,
    commentFooter,
  },
};
</script>

<style lang="less" scoped>
.lists {
  border-top: 5px solid #ddd;
  padding: 0 15px 50px;
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: 50/360 * 100vw;
        height: 50/360 * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
}
</style>
